#!/usr/bin/python3
# -*- encoding:utf-8 -*-
# author: king
# create date time: 2024/4/24 上午7:13

import datetime


class CalculateTimeDifference(object):
    """
    计算时间差类
    """

    @staticmethod
    def time_difference(source_time_str: str, target_time_str: str) -> datetime.timedelta:
        """
        计算时间差
        :return:
        :author: king
        :create date time: 2024/4/24 上午7:19
        """
        c_datetime = datetime.datetime.now().strftime("%Y%m%d")  # 当前日期字符串
        source_time = datetime.datetime.strptime(f"{c_datetime}{source_time_str}", "%Y%m%d%H%M%S")
        target_time = datetime.datetime.strptime(f"{c_datetime}{target_time_str}", "%Y%m%d%H%M%S")
        return target_time - source_time

    @staticmethod
    def date_difference(source_date_str: str, target_date_str: str) -> int:
        """
        计算日期差
        :return: 相差的天数
        :author: king
        :create date time: 2024/4/24 上午7:26
        """
        source_date = datetime.datetime.strptime(source_date_str, "%Y%m%d")
        target_date = datetime.datetime.strptime(target_date_str, "%Y%m%d")
        return (target_date - source_date).days

    @staticmethod
    def datetime_difference(source_datetime_str: str, target_datetime_str: str) -> datetime.timedelta:
        """
        :return:
        :author: king
        :create date time: 2024/4/24 上午7:31
        """
        source_datetime = datetime.datetime.strptime(source_datetime_str, "%Y%m%d%H%M%S")
        target_datetime = datetime.datetime.strptime(target_datetime_str, "%Y%m%d%H%M%S")
        return target_datetime - source_datetime


def main():
    """程序入口"""

    source_time_str = "091500"
    target_time_str = "151500"
    ctd = CalculateTimeDifference()
    time_diff = ctd.time_difference(source_time_str, target_time_str)
    date_diff = ctd.date_difference("20230115", "20230215")
    datetime_diff = ctd.datetime_difference("20230115091500", "20230215153000")
    print(datetime_diff)


if __name__ == "__main__":
    main()
